package selection;

import java.util.ArrayList;
import java.util.Random;

import model.Chromosome;

public class Roulette implements Selection {

	Random rand=new Random();
	@Override
	public Chromosome[] selectPatents(ArrayList<Chromosome> population) {

		int sum=0;
		for (Chromosome chromosome : population) {
			sum+=chromosome.getFitness();	
		}
		int flag=rand.nextInt(sum);
		int sum2=0;
		Chromosome parent1=null;
		for (Chromosome chromosome : population) {
			sum2+=chromosome.getFitness();
			if(flag<sum2)
			{
				parent1 = chromosome;
				break;
			}
		}
		Chromosome parent2=null;
		flag=rand.nextInt(sum);
		sum2=0;
		for (Chromosome chromosome : population) {
			sum2+=chromosome.getFitness();
			if(flag<sum2)
			{
				parent2 = chromosome;
				break;
			}
		}
		return new Chromosome[] {parent1,parent2};
	}
	@Override
	public String toString() {
		return "Roulette";
	}

}
